# -*- coding: utf-8 -*-

# Copyright 2013 Ida Leter, adapted from Fanficdownloader
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Correct way of reading from IEEE since it does not allow normal urlib operations without user agent
##url = 'http://dl.acm.org/citation.cfm?id=331403.331405'
##req = u2.Request(url, headers={'User-Agent' : "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1"})
##try:
##    data = u2.urlopen(req).read()


import time
import logging
logger = logging.getLogger(__name__)
import re
import urllib2

from .. import BeautifulSoup as bs
from .. import exceptions as exceptions
from ..htmlcleanup import stripHTML

from base_adapter import BaseSiteAdapter,  makeDate

def getClass():
    return IEEEAdapter


logger = logging.getLogger(__name__)

class IEEEAdapter(BaseSiteAdapter):

    def __init__(self, config, url):
        BaseSiteAdapter.__init__(self, config, url)

        self.decode = ["utf8","Windows-1252"]
        self.username = "" # if left empty, site doesn't return any message at all.
        self.password = ""

        # get paperId
        try:
            paperId = self.parsedUrl.query.split('arnumber=',)[1]
            if '&' in paperId:
                paperId = paperId.split('&')[0]
            self.paper.setMetadata('paperId',paperId)
        except:
            self.paper.setMetadata('paperId',self.parsedUrl.path.split('/',)[2])
        preUrl = self.parsedUrl.path.split('/',)[1]
        # normalized URL.
        self._setURL('http://' + self.getSiteDomain() + '/' + preUrl +'/abs_all.jsp?arnumber='+self.paper.getMetadata('paperId'))

        # Each adapter needs to have a unique site abbreviation.
        self.paper.setMetadata('siteabbrev','ieee')
        
        # The date format will vary from site to site.
        # http://docs.python.org/library/datetime.html#strftime-strptime-behavior
        self.dateformat = "%Y"

    @staticmethod # must be @staticmethod, don't remove it.
    def getSiteDomain():
        # The site domain.  Does have www here, if it uses it.
        return 'ieeexplore.ieee.org'

    def getSiteExampleURLs(self):
        return "http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=6491783"

    def getSiteURLPattern(self):
        return r"http://ieeexplore.ieee.org/*.*/.*arnumber=\d+.*"


    def needToLoginCheck(self, data):
        if 'Full text access may be available' in data:
            return True
        else:
            return False


    def extractMetadata(self):
        try:
            data = self._fetchUrl(self.url)
            
        except urllib2.HTTPError, e:
            if e.code == 404:
                raise exceptions.PaperDoesNotExist()
            else:
                raise e


        # use BeautifulSoup HTML parser to make everything easier to find.
        soup = bs.BeautifulSoup(data)
        
        # Now go hunting for all the meta data and the chapter list.
        # Title
        a = soup.find('h1')
        self.paper.setMetadata('title'," ".join(stripHTML(a).split()))

        # Author
        alist = soup.findAll('span', {'id':'preferredName'})
        for a in alist:
            self.paper.addToList('author',a['class'])
        if self.paper.getMetadata('author') == None:
            self.paper.addToList('author','IEEE')

        # Date
        try:
            a = " ".join(stripHTML(soup.find(text=re.compile("Date")).parent.nextSibling.string).split())
            i=len(a)
            self.paper.setMetadata('datePublished', makeDate(a[i-4:], self.dateformat))
        except:
            a = " ".join(stripHTML(soup.find(text=re.compile("Year")).parent.nextSibling.nextSibling.string).split())
            i=len(a)
            self.paper.setMetadata('datePublished', makeDate(a[i-4:], self.dateformat))
        

        # Abstract
        a = soup.find('div',{'class':'article'})
        self.paper.setMetadata('abstract',stripHTML(a))


        # pdf link
        if self.needToLoginCheck(data):
            # need to log in for this one.
            print "Logging in is not supported. No pdf is downloaded"
        else:
            pdf = 'http://' + self.getSiteDomain() + '/stamp/stamp.jsp?tp=&arnumber='+self.paper.getMetadata('paperId')
            self.paper.setMetadata('pdfurl', pdf)

        # doi
        a = soup.find('a', href=re.compile(r"http://dx.doi.org"))
        if a != None:
            self.paper.setMetadata('doi',a.text)

        # Publisher
        try:
            a = stripHTML(soup.find(text=re.compile("Published in")).parent.nextSibling.nextSibling.nextSibling)
        except:
            try:
                a = stripHTML(soup.find(text=re.compile("Book Title")).parent.nextSibling.nextSibling)
                a = stripHTML(soup.find(text=re.compile("Publisher")).parent.nextSibling.nextSibling)
                a=a+' ('+b+')'
            except:
                a='IEEE'
        self.paper.setMetadata('publisher',a)
